#ifndef _UTIL_THREADPOOL_H_
#define _UTIL_THREADPOOL_H_

#include "common/common.h"
#include "util/Thread.h"


class ThreadPool {
	public:
		ThreadPool(uint32_t capability = 0, uint32_t stack_size = 0);
		~ThreadPool();
	public:
		ret_t newThread(Thread ** thrp, Runnable * runner, bool block = true);
		ret_t freeThread(Thread * thr);
		ret_t terminateThread(Thread * thr);

		void join();
		void interrupt();
		void terminate();
	public:
		uint32_t getFreeCount() const { return _freecnt; }
		uint32_t getBusyCount() const { return _busycnt; }
		uint32_t getTerminatedCount() const { return _termcnt; }
		uint32_t size() const { return _size; }
		uint32_t capability() const { return _capability; }
		bool isTerminated() const { return _terminated; }
	private:
		Thread * _freelist;
		uint32_t _freecnt;
		Thread * _busylist;
		uint32_t _busycnt;
		Thread * _termlist;
		uint32_t _termcnt;
		uint32_t _size;
		uint32_t _capability;
		uint32_t _stack_size;
		uint32_t _stack_guard_size;
		bool _terminated;
		Conditions _conds;
};

typedef ThreadPool CThreadPool;


#endif //_UTIL_THREADPOOL_H_
